package com.hnkywl.sts.service.sys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.sys.DsgdDao;
import com.hnkywl.sts.entity.organ.Dsgd;
import com.hnkywl.sts.entity.organ.Gsjg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-20
 * Time: 下午7:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DsgdService {
    private DsgdDao dsgdDao;
    private GsjgDao gsjgDao;

    public String getCggsFromKh() {
        List<Map> dsGdList = dsgdDao.findCggsFromKh();
        return new Gson().toJson(dsGdList);
    }

    @Transactional
    public void saveDsgdList(String nbgsIds, String hours, String minutes, String seconds) {
        String[] gdStrs = nbgsIds.split(",");
        if (gdStrs.length > 0) {
            for (String str : gdStrs) {
                Gsjg nbgs = gsjgDao.get(Long.parseLong(str));
                Dsgd dsgd = new Dsgd();
                dsgd.setNbgs(nbgs);
                dsgd.setHours(hours);
                dsgd.setMinutes(minutes);
                dsgd.setSeconds(seconds);
                dsgdDao.save(dsgd);
            }
        }
    }

    @Transactional
    public void deleteDsgdData() {
        dsgdDao.deleteData();
    }

    @Transactional
    public List<Long> findDsgdGsIds() {
        List<Long> temps = new ArrayList<Long>();
        List<Dsgd> dsgdList = dsgdDao.findAll();
        if (dsgdList != null && dsgdList.size() > 0) {
            for (Dsgd dsgd : dsgdList) {
                temps.add(dsgd.getNbgs().getId());
            }
        }
        return temps;
    }

    @Autowired
    public void setDsgdDao(DsgdDao dsgdDao) {
        this.dsgdDao = dsgdDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
